package com.example.springboottaskscheduler.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
/**
 * Created by IntelliJ IDEA2022.3.2
 * @Author: Tenghw
 * @Date: 2023/05/07  18:20
 * @Description: 定时任务调度器自定义配置
 */
@Configuration
public class CronTaskSchedulerConfig {

    /**
     * 定时任务调度器自定义配置
     */
    @Bean(name = "tenghw-taskScheduler")
    public TaskScheduler taskScheduler() {
        // 定时任务调度器线程池
        ThreadPoolTaskScheduler poolTaskScheduler = new ThreadPoolTaskScheduler();
        // 线程池核心线程数：可同时执行4个任务
        poolTaskScheduler.setPoolSize(4);
        // 启用 取消定时任务策略
        poolTaskScheduler.setRemoveOnCancelPolicy(true);
        // 线程池中线程前缀
        poolTaskScheduler.setThreadNamePrefix("pool-taskScheduler-");
        return  poolTaskScheduler;
    }
}
